package Object;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author jklu
 * 对象--->通过百度Flip接口返回的一页，包含一页中所有图片对象
 */

public class BaiduPic2 {
    //图片集合
    private List<Pic> picList = new ArrayList<Pic>();

    public List<Pic> getPicList() {
        return picList;
    }

    //构造函数
    public BaiduPic2(String document){
        Pattern p = Pattern.compile("\"pageNum\":(\\d+),\\s*?\"objURL\":\"([^\"]+)\".+?\"width\":(\\d+),\\s*?\"height\":(\\d+),\\s*?\"type\":\"(\\w{3,4})\",.+?\"fromPageTitle\":\"([^\"]+)\"");
        Matcher m = p.matcher(document);
        while (m.find()){
            //依次获取图片信息
            int number = Integer.parseInt(m.group(1));
            String url = m.group(2);
            int width = Integer.parseInt(m.group(3));
            int height = Integer.parseInt(m.group(4));
            String type = m.group(5);
            //去除特殊符号
            String regex = "[`~!@#$%^&*()_\\-+=<>?:\"{}|,.\\\\/;']";
            //去除HTML标签
            String title = m.group(6).replaceAll("<\\\\?/?\\w+>","").replaceAll(regex,"");
            if(title.isEmpty())
                title = String.valueOf(number)+String.valueOf(width)+String.valueOf(height);
            picList.add(new Pic(number,width,height,title,url,type));
        }
    }
}
